
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../hj_model/home/module_detail_model.dart';
import '../../../hj_model/home/video_list_resp.dart';
import '../../../hj_utils/api_service/vid_service.dart';
import '../../../tools_base/debug_log.dart';

class SectionAllLogic extends GetxController with GetTickerProviderStateMixin {

  final String id;

  SectionAllLogic(this.id);

  int currentPage = 1;
  List<AllSection>? dataSource;
  RefreshController? refreshController;
  @override
  void onInit() {
    super.onInit();
    _loadData();
  }


  void onInitData() {
    dataSource = null;
    update();
    _loadData();
  }

  void refreshData() {
    _loadData();
  }

  void loadMoreData() {
    _loadData(page: currentPage + 1);
  }

  void _loadData({int page = 1,  int size = 10}) async {
    try {
      SectionListResp? retResp = await VidService.fetchSectionAll(id ?? '', pageNumber: page, pageSize: size);
      currentPage = page;
      dataSource ??= [];
      if (currentPage == 1) {
        dataSource?.clear();
      }
      dataSource?.addAll(retResp?.list ?? []);
      if (retResp?.hasNext == true) {
        refreshController?.loadComplete();
      } else {
        refreshController?.loadNoData();
      }
    } catch (e) {
      dataSource ??= [];
      debugLog(e);
      refreshController?.loadComplete();
    }
    refreshController?.refreshCompleted();
    update();
  }
}
